import Vue from "vue"
import VueRouter from "vue-router"

Vue.use(VueRouter)

const router = new VueRouter({
  routes:[
    { path: '/', redirect: '/home' },
    { path: '/login', component: () => import('@/components/Login.vue') },
    { 
      path: '/home',
      component: () => import('@/components/Home.vue'),
      children: [
        { path: '/', redirect: '/user' },
        { path: '/good', component: () => import('@/components/menus/Good.vue') },
        { path: '/order', component: () => import('@/components/menus/Order.vue') },
        { path: '/right', component: () => import('@/components/menus/Right.vue') },
        { path: '/setting', component: () => import('@/components/menus/Setting.vue') },
        { path: '/user', component: () => import('@/components/menus/User.vue') },
        { path: '/userDetail/:id', component: () => import('@/components/user/UserDetail.vue') }
      ]
    }
  ]
})

router.beforeEach((to, from, next) => {
  if(to.path === '/login') {
    next()
  } else {
    const token = localStorage.getItem('token')
    if(token) {
      next()
    } else {
      next('/login')
    }
  }
})

export default router